package com.accp.hotevent.controller;

import com.accp.hotevent.entity.Comment;
import com.accp.hotevent.entity.Event;
import com.accp.hotevent.entity.User;
import com.accp.hotevent.req.AddCommentRequest;
import com.accp.hotevent.req.CommentlistRequest;
import com.accp.hotevent.resp.CommonResp;
import com.accp.hotevent.resp.LoginUserResp;
import com.accp.hotevent.service.CommentService;
import com.accp.hotevent.util.LoggedUserContext;
import com.github.pagehelper.PageInfo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    public CommonResp<PageInfo<Comment>> getCommentlist(CommentlistRequest request){
        Comment comment = new Comment();
        if(request.getUserId() != null) {
            comment.setUser(User.builder().id(request.getUserId()).build());
        }
        if(request.getEventId() != null) {
            comment.setEvent(Event.builder().id(request.getEventId()).build());
        }
        PageInfo<Comment> pageInfo = this.commentService.getCommentsList(request.getPageNum(), request.getPageSize(), comment);
        CommonResp<PageInfo<Comment>> commonResp = new CommonResp<>();
        commonResp.setSuccess(true);
        commonResp.setData(pageInfo);
        return commonResp;
    }

    @GetMapping("/inpage")
    public CommonResp<Integer> getCommentInPage(Integer eventId,Integer commentId,Integer pageSize){
        CommonResp<Integer> commonResp = new CommonResp<>();
        Comment comment = new Comment();
        comment.setEvent(Event.builder().id(eventId).build());
        int page = 0;
        PageInfo<Comment> pageInfo = this.commentService.getCommentsList(1, 99999, comment);
        List<Comment> comments = pageInfo.getList();
        for (int i = 0; i < comments.size(); i++){
            if(comments.get(i).getId() == commentId){
                if((i+1)%pageSize==0){
                    page = (i+1) / pageSize;
                }else{
                    page = (i+1) / pageSize + 1;
                }
                break;
            }
        }
        commonResp.setSuccess(true);
        commonResp.setData(page);
        return commonResp;
    }

    @PostMapping("/add")
    public CommonResp<Comment> addComment(@RequestBody AddCommentRequest commentRequest){
        User user = LoggedUserContext.getCurrentLoggedUser();
        if(user.isBanned()){
            throw new RuntimeException("该用户已被禁言");
        }
        CommonResp<Comment> commonResp = new CommonResp<>();

        Comment comment = new Comment();
        comment.setContent(commentRequest.getContent());
        comment.setEventId(commentRequest.getEventId());
        comment.setUser(user);
        commentService.add(comment);
        commonResp.setSuccess(true);
        commonResp.setData(comment);
        return commonResp;
    }
}
